package Ui.guiComponents;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
//import java.util.Iterator;
//import java.util.LinkedList;
//import java.util.List;
import java.util.*;
//import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.plaf.basic.BasicSliderUI.ComponentHandler;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Elya
 */

public class MainWindow extends JFrame {
    
    
    /** Creates new form MainWindow */
    private static UiData PlayersData;
    private Vector boardPanelVector = new Vector();
    private int indexInBoardVector;
    private BoardPanel board;
    MainListener mwListener;
              
    public MainWindow() {
       
        PlayersData= new UiData();
        initComponents();
        indexInBoardVector=0;
        board=new BoardPanel();
        board.setVisible(false);
        mwListener= new MainListener(this);
    }

    
  
    public void addBoardPanel(){
        board=new BoardPanel();
         boardPanelVector.add(board);   
         //board = (BoardPanel)boardPanelVector.elementAt(indexInBoardVector);
         JTextField turn = new JTextField();
         String str=new String();
         str=str+('0' + indexInBoardVector);
         turn.setText(str);
         board.setTextPlayerTurn(turn);
         indexInBoardVector++;
         board.setVisible(false);
     
    }
    
    public MainListener getMwListener() {
       
        return mwListener;
    }
    
    public void setCurrentBoardVisibale(){
        
        board.setAllVisible(true);
    }
    
    public static void createAndRunJFrame() {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
       
                MainWindow mw=new MainWindow();
                mw.setSize(800,750);

                mw.getContentPane().setLayout(new BorderLayout());
                BoardPanel boardPanel=mw.board;
                Header header=new Header();
                Players players = new Players(PlayersData,mw);
                SetShips setships=new SetShips();
                MainListener mwListener= mw.getMwListener();
                //boardPanel.setAllVisible(false);
                
                mw.getContentPane().add(header,BorderLayout.PAGE_START);
                mw.getContentPane().add(mw.board,BorderLayout.CENTER);
                mw.getContentPane().add(players,BorderLayout.WEST);
                mw.getContentPane().add(setships,BorderLayout.EAST);

               
                mw.setVisible(true);
                mw.setLocationRelativeTo(null);            
           }
        });
    }

    public Vector getBoardPanelVector() {
            return boardPanelVector;
        }    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JMenu = new javax.swing.JMenuBar();
        File = new javax.swing.JMenu();
        JCustomData = new javax.swing.JMenuItem();
        JLoadData = new javax.swing.JMenuItem();
        JSaveData = new javax.swing.JMenuItem();
        JQuit = new javax.swing.JMenuItem();
        Edit = new javax.swing.JMenu();
        JUndo = new javax.swing.JMenuItem();
        JRedo = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Battleship");
        setAlwaysOnTop(true);
        setName("Battleship"); // NOI18N
        setResizable(false);

        File.setText("File");

        JCustomData.setText("Create custom game settings");
        File.add(JCustomData);

        JLoadData.setText("Load game settings from existing file");
        JLoadData.setActionCommand("Load settings from existing file");
        JLoadData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JLoadDataActionPerformed(evt);
            }
        });
        File.add(JLoadData);

        JSaveData.setText("Save current game settings");
        File.add(JSaveData);

        JQuit.setText("Quit Game");
        File.add(JQuit);

        JMenu.add(File);

        Edit.setText("Edit");

        JUndo.setText("Undo");
        JUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JUndoActionPerformed(evt);
            }
        });
        Edit.add(JUndo);

        JRedo.setText("Redo");
        Edit.add(JRedo);

        JMenu.add(Edit);

        setJMenuBar(JMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 832, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleName("Main");

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void JUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JUndoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_JUndoActionPerformed

private void JLoadDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JLoadDataActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_JLoadDataActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Edit;
    private javax.swing.JMenu File;
    private javax.swing.JMenuItem JCustomData;
    private javax.swing.JMenuItem JLoadData;
    private javax.swing.JMenuBar JMenu;
    private javax.swing.JMenuItem JQuit;
    private javax.swing.JMenuItem JRedo;
    private javax.swing.JMenuItem JSaveData;
    private javax.swing.JMenuItem JUndo;
    // End of variables declaration//GEN-END:variables
}
